WPF 已知问题 dotnet 6 设置 InvariantGlobalization 之后将丢失默认绑定转换导致 XAML 抛出异常 |
您所在的位置:网站首页 › xaml binding › WPF 已知问题 dotnet 6 设置 InvariantGlobalization 之后将丢失默认绑定转换导致 XAML 抛出异常 |
在设置了 InvariantGlobalization 为 true 之后,将会发现原本能正常工作的 XAML 可能就会抛出异常。本文将告诉大家此问题的原因 这是有开发者在 WPF 仓库上给我报告的 bug 我才找到的问题。问题的现象是 XAML 抛出异常,步骤有些复杂: 升级到 dotnet 6 版本。 因为此问题是在 dotnet 6 下才能复现,在 dotnet 6 以下,如 dotnet 5 和 dotnet core 3.1 是没有问题的要求设置 InvariantGlobalization 为 true 的值在 XAML 绑定静态的非字符串类型的属性,例如 int 类型的属性,如以下代码这是 MainWindow.xaml.cs 的代码: using System.Windows; namespace repro { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } public static string IWillNotCauseException { get; set; } public static int IWillCauseException { get; set; } } }这是在 XAML 的代码 运行之后,将会看到 XAML 抛出异常。详细请看 https://github.com/dotnet/wpf/issues/6477 抛出的异常包含以下信息 System.Globalization.CultureNotFoundException: 'Only the invariant culture is supported in globalization-invariant mode. See https://aka.ms/GlobalizationInvariantMode for more information. (Parameter 'name')原因是在 dotnet 6 设置了 InvariantGlobalization 为 true 之后,在调用 CultureInfo.GetCultureInfoByIetfLanguageTag 方法时,将抛出异常,如下图 这是符合 官方文档 描述的 如 Breaking change: Culture creation and case mapping in globalization-invariant mode 文档所述: Starting in .NET 6 when globalization-invariant mode is enabled: If an app attempts to create a culture that's not the invariant culture, a CultureNotFoundException exception is thrown.因此这个问题其实是 dotnet 6 的符合预期的行为,也不是 WPF 的问题 附设置 InvariantGlobalization 为 true 的方法如下 编辑 csproj 项目文件,添加 true 到 PropertyGroup 里面,如以下代码 true或者是编辑 runtimeconfig.json 文件,添加如下代码 { "runtimeOptions": { "configProperties": { "System.Globalization.Invariant": true } } } |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |